package yajhfc.send;
/*
 * YAJHFC - Yet another Java Hylafax client
 * Copyright (C) 2005-2011 Jonas Wolz <info@yajhfc.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Linking YajHFC statically or dynamically with other modules is making 
 *  a combined work based on YajHFC. Thus, the terms and conditions of 
 *  the GNU General Public License cover the whole combination.
 *  In addition, as a special exception, the copyright holders of YajHFC 
 *  give you permission to combine YajHFC with modules that are loaded using
 *  the YajHFC plugin interface as long as such plugins do not attempt to
 *  change the application's name (for example they may not change the main window title bar 
 *  and may not replace or change the About dialog).
 *  You may copy and distribute such a system following the terms of the
 *  GNU GPL for YajHFC and the licenses of the other code concerned,
 *  provided that you include the source code of that other code when 
 *  and as the GNU GPL requires distribution of source code.
 *  
 *  Note that people who make modified versions of YajHFC are not obligated to grant 
 *  this special exception for their modified versions; it is their choice whether to do so.
 *  The GNU General Public License gives permission to release a modified 
 *  version without this exception; this exception also makes it possible 
 *  to release a modified version which carries forward this exception.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import yajhfc.DateKind;
import yajhfc.Utils;
import yajhfc.file.FormattedFile;

public class StreamTFLItem extends HylaTFLItem {
    protected FormattedFile tempFile;
    protected String text;
    
    @Override
    public boolean equals(Object obj) {
        return (obj == this);
    }
    
    @Override
    public void cleanup() {
        tempFile.file.delete();
        tempFile = null;
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    public boolean isMutable() {
        return false;
    }
    
    @Override
    public boolean isDeletable() {
        return true;
    }
    
    @Override
    public FormattedFile getPreviewFilename() {
        return tempFile;
    }

    @Override
    public void setText(String newText) {
        throw new IllegalArgumentException("StreamTFLItem is immutable!");
    }
    

    /**
     * Creates a new StreamTFLItem by copying the specified stream to a temporary
     * file.
     * @param inStream
     * @param streamSource textual description of stream source for user display or null for a default text
     * @throws IOException
     * @throws FileNotFoundException
     */
    public StreamTFLItem(InputStream inStream, String streamSource) throws IOException, FileNotFoundException {
        File tmp;
        // Copy input stream to a temporary file:
        tmp = File.createTempFile("submit", ".ps");
        tmp.deleteOnExit();
        FileOutputStream fOut = new FileOutputStream(tmp);
        Utils.copyStream(inStream, fOut);
        fOut.close();
        
        tempFile = new FormattedFile(tmp);
        if (streamSource == null)
            streamSource = Utils._("<stdin>");
        text = streamSource + " – " + DateKind.TIME_ONLY.getFormat().format(new Date());
    }
    
}
